package com.kotlin.flong.coroutines1

import kotlinx.coroutines.newSingleThreadContext
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext

/**
 * @Author:liangjl
 * @Date: 2020/7/8-21:18
 * @Eamil:jilongliang@sina.com
 * @Description:
 */

private fun log(logMsg: String) = println("${Thread.currentThread().name} $logMsg")

fun main() {

    newSingleThreadContext("Context1").use { cxt1 ->
        newSingleThreadContext("Context2").use { cxt2 ->
            runBlocking(cxt1) {
                log("start in context1 ")
                withContext(cxt2) {
                    log("working in context2 ")
                }

                log("back in context1 ")
            }
        }

    }
}